package xyz.lwm.lazycat.netpipe.service.impl;

import org.mindrot.jbcrypt.BCrypt;
import xyz.lwm.lazycat.annotation.Inject;
import xyz.lwm.lazycat.netpipe.domain.entity.User;
import xyz.lwm.lazycat.netpipe.domain.enums.TokenType;
import xyz.lwm.lazycat.netpipe.domain.vo.Token;
import xyz.lwm.lazycat.netpipe.service.AuthService;
import xyz.lwm.lazycat.netpipe.service.UserService;
import xyz.lwm.lazycat.netpipe.util.Tokens;
import xyz.lwm.lazycat.utility.Assert;
import xyz.lwm.lazycat.web.annocation.Service;

/**
 * @author lwm
 */
@Service
class AuthServiceImpl implements AuthService {

    @Inject
    private UserService userService;

    @Override
    public Token login(User user) {
        User existed = userService.getByUsername(user.getUsername());
        Assert.isTrue(existed != null, "用户不存在");
        Assert.isTrue(BCrypt.checkpw(user.getPassword(), existed.getPassword()), "密码错误");
        return Tokens.create(TokenType.DEFAULT, existed.getId());
    }

}
